package com.woniuxy.controller;


import com.woniuxy.pojo.Linkman;
import com.woniuxy.service.LinkmanService;
import com.woniuxy.service.MyLog;
import com.woniuxy.util.DataGridView;
import com.woniuxy.util.ResultDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
public class LinkmanController {
    private Logger logger= LoggerFactory.getLogger(this.getClass());
    @Autowired
    private LinkmanService linkmanService;
    @GetMapping(value = "linkmans/selectLinkman")
    public DataGridView selectLinkman(String linkCustNo){
        //logger.debug("用户id:"+linkCustNo);
        return linkmanService.selectLinkmanByLink(linkCustNo);
    }
    //增加联系人
    @MyLog(value = "增加联系人")  //这里添加了AOP的自定义注解
    @PostMapping(value = "/linkmans.do")
    public ResultDto addLinkman(Linkman linkman) {
        linkman.setLinkNo("lk" + new Date().getTime());
        Linkman linkmanResult= this.linkmanService.selectLinkmanBylinkNo(linkman.getLinkNo());
        if(linkmanResult!=null) {
            return ResultDto.USER_EXITS;
        }else {
            boolean flag=this.linkmanService.addLinkman(linkman);
            if (flag) {
                return ResultDto.USER_ADD_SUCCESS;
            } else {
                return ResultDto.USER_ADD_FAILURE;
            }
        }
    }

    //删除联系人
    @MyLog(value = "删除联系人")  //这里添加了AOP的自定义注解
    @DeleteMapping(value = "/linkmans/{linkNo}")
    public ResultDto deleteLinkman(@PathVariable("linkNo") String linkNo){
        try {
            this.linkmanService.deleteLinkmanBylinkNo(linkNo);
            return ResultDto.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_FAILURE;
        }
    }
    //批量删除联系人
    @MyLog(value = "批量删除联系人")  //这里添加了AOP的自定义注解
    @GetMapping(value = "linkmans/deleteBatch")
    public ResultDto deleteBathLinkman(@RequestParam("linkNos[]") String[] linkNos){
        try {
            this.linkmanService.removeBatchLinkman(linkNos);
            return ResultDto.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.DELETE_FAILURE;
        }
    }
    //更新顾客信息
    @MyLog(value = "更新顾客信息")  //这里添加了AOP的自定义注解
    @PostMapping(value = "linkmans/update")
    public ResultDto updateLinkman(Linkman linkman){
        try {
            this.linkmanService.updateLinkman(linkman);
            return ResultDto.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultDto.UPDATE_FAILURE;
        }
    }
}
